package com.shengyuan.manage_os.old.ying.erpm.controls;

import java.text.SimpleDateFormat;
import java.util.Date;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.os.Parcelable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.TextView;

public class CurrentTime extends TextView {

	public static Boolean Stop=false;
	@Override
	public Parcelable onSaveInstanceState() {
		// TODO Auto-generated method stub
		return super.onSaveInstanceState();
	}
	@Override
	protected void onVisibilityChanged(View changedView, int visibility) {
		if (visibility==1) {
			
		}
		super.onVisibilityChanged(changedView, visibility);
	}
	public CurrentTime(Context context) {
		super(context);

	}
	public CurrentTime(Context context, AttributeSet attrs) {
		super(context,attrs);
		try {
			StartTime(uiHandler1);
		} catch (Exception e) {
		}
	}
	
	@SuppressLint("HandlerLeak")
	private Handler uiHandler1 = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			String text = getTimeString();
			setText(text);
			if (Stop) { //强制外部清除计时
				return;
			}
			StartTime(uiHandler1);
		}
	};
	
	private void StartTime(Handler handler) {
		if (handler!=null) {
			handler.sendEmptyMessageDelayed(1, 1 * 1000);
		}
	}
	
	public static String getTimeString() {
			SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");      
			String text = formatter.format(getTime());    
			return text;
	}
	
	public static String getTimeString(Date time) {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");      
		String text = formatter.format(time);    
		return text;
}

	public static String CurrentTimeMillis;
	public static Date getTime() { 
		long l = System.currentTimeMillis();
		CurrentTimeMillis = l +"";
		Date curDate = new Date(l);//获取当前时间   
		return curDate;
	}
	
	public static String getShortTime() {
		SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");      
		String text = formatter.format(getTime());    
		return text;
	}
	public static String getShortTime(String longtime) {
		SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");  
		Date date = new Date(longtime);
		String text = formatter.format(date);    
		return text;
	}
	public static String getLongTime() {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");      
		String text = formatter.format(getTime());    
		return text;
	}
}
